package com.geostar.constructionland.analyse.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.geostar.constructionland.analyse.mapper.sptalyresult.AnalyseVersionMapper;
import com.geostar.constructionland.common.entity.analyse.AnalyseVersion;
import com.geostar.constructionland.common.entity.response.ExtJsAjaxResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * @author yelll
 * @since 2020-5-26 18:30
 **/
@RestController
@RequestMapping("/analyseVersion")
@Api(value = "分析版本接口")
@Slf4j
public class AnalyseVersionController {
    @Autowired
    AnalyseVersionMapper alyVerMapper;

    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "添加版本信息")
    public ExtJsAjaxResponse addAnalyseVersion(@RequestBody @Valid AnalyseVersion analyseVersion) {
        /**增加发布时间**/
        analyseVersion.setPublicTime(new Date());
        alyVerMapper.insert(analyseVersion);
        return ExtJsAjaxResponse.createBySuccessMessage("添加分析版本信息成功!");
    }

    @PostMapping("/del")
    @ResponseBody
    @ApiOperation(value = "删除版本信息")
    @ApiImplicitParam(name = "versionId", value = "版本ID")
    public ExtJsAjaxResponse delAnalyseVersion(@NotBlank(message = "版本ID不能为空") @RequestParam(value = "versionId") String versionId) {
        if (versionId.contains(",")) {
            // 将多个id处理成集合形式
            List<String> versionIds = Arrays.asList(versionId.split(","));
            alyVerMapper.deleteBatchIds(versionIds);
        } else {
            alyVerMapper.deleteById(versionId);
        }
        return ExtJsAjaxResponse.createBySuccessMessage("删除版本信息成功!");
    }

    @PostMapping("/update")
    @ResponseBody
    @ApiOperation(value = "更新版本信息")
    public ExtJsAjaxResponse updateAnalyseVersion(@RequestBody @Valid AnalyseVersion analyseVersion) {
        alyVerMapper.updateById(analyseVersion);
        return ExtJsAjaxResponse.createBySuccessMessage("更新版本信息成功!");
    }

    @PostMapping("/getAnalyseVersions")
    @ResponseBody
    @ApiOperation(value = "获取分析版本信息")
    public ExtJsAjaxResponse getAnalyseVersions(@NotBlank(message = "页码不能为空") @RequestParam(value = "page") int pageNo,
                                                @NotBlank(message = "每页数量不能为空") @RequestParam(value = "limit") int pageNum,
                                                String analyseType,
                                                String analyseName,
                                                String query_analyse_name) throws Exception {
        IPage<AnalyseVersion> analyseVersionIPage = new Page<>(pageNo, pageNum);
        QueryWrapper<AnalyseVersion> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isBlank(analyseType)) {
            queryWrapper.like("analyse_type", analyseType);
        }
//        if(!StringUtils.isBlank(analyseName)){
//            queryWrapper.like("analyse_name",analyseName);
//        }
        if (!StringUtils.isBlank(query_analyse_name)) {
            queryWrapper.like("analyse_name", query_analyse_name);
        }
        queryWrapper.orderByDesc("PUBLIC_TIME");
        analyseVersionIPage = alyVerMapper.selectPage(analyseVersionIPage, queryWrapper);
        ExtJsAjaxResponse extJsAjaxResponse = ExtJsAjaxResponse.createBySuccess(analyseVersionIPage);
        log.info("ExtJsResponse:{}", new ObjectMapper().writeValueAsString(extJsAjaxResponse));
        return extJsAjaxResponse;
    }
}
